<?php

namespace App\Containers\AppSection\Questionnaire\Models;

use App\Ship\Parents\Models\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

/**
 * Ответ
 *
 * @property int $id Id
 * @property int $questionnaire_id Анкета
 * @property int $form_id Форма
 *
 * @property Questionnaire $questionnaire Анкета
 * @property Value[] $values Поля
 * @property Form $form Форма
 */
class Response extends Model
{
    public $timestamps = false;
    protected $fillable = [
        'questionnaire_id',
        'form_id',
    ];
    protected $dates = [
        'created_at',
        'updated_at',
    ];
    /**
     * A resource key to be used in the serialized responses.
     */
    protected string $resourceKey = 'Response';
    protected $table = 'questionnaire_responses';

    /**
     * Форма
     * @return BelongsTo Form
     */
    public function form(): BelongsTo
    {
        return $this->belongsTo(Form::class, 'form_id', 'id');
    }

    /**
     * Анкета
     * @return BelongsTo Questionnaire
     */
    public function questionnaire(): BelongsTo
    {
        return $this->belongsTo(Questionnaire::class, 'questionnaire_id', 'id');
    }

    /**
     * Поля
     * @return HasMany Values
     */
    public function values(): HasMany
    {
        return $this->hasMany(Value::class, 'response_id', 'id')->orderBy('person')->orderBy('field_id');
    }
}
